﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using System.Media;

namespace DoAnHuongRung
{
    public partial class frmNhapKhoNVL : Form
    {
        public frmNhapKhoNVL()
        {
            InitializeComponent();
        }

        private void txtTimKiem_Click(object sender, EventArgs e)
        {
           if (txtTimKiem.Text.Equals("Tìm kiếm"))
            {
                txtTimKiem.Text = "";
                txtTimKiem.ForeColor = Color.Black;
            }   
        }

        private void LoadNguyenVatLieu()
        {
            dtgNguyenVatLieu.AutoGenerateColumns = false;
            dtgNguyenVatLieu.DataSource = NguyenVatLieuBUS.SelectAll();
        }

        private void frmNhapKhoNVL_Load(object sender, EventArgs e)
        {
            LoadNguyenVatLieu();
        }

        private void dtgNguyenVatLieu_SelectionChanged(object sender, EventArgs e)
        {
            lblTenNVL.Text = dtgNguyenVatLieu.CurrentRow.Cells[1].Value.ToString();
        }

        SoundPlayer s;

        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                NguyenVatLieuDTO nvl = new NguyenVatLieuDTO();
                nvl.MaNVL = int.Parse(dtgNguyenVatLieu.CurrentRow.Cells[0].Value.ToString());
                nvl.SoLuong = int.Parse(txtSoLuong.Value.ToString());
                bool b = NguyenVatLieuBUS.Insert_SoLuong(nvl);
                LoadNguyenVatLieu();
            }
            catch (Exception ex)
            {
                s = new SoundPlayer(Properties.Resources.Windows_Error);
                s.Play();
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            try
            {
                NguyenVatLieuDTO nvl = new NguyenVatLieuDTO();
                nvl.MaNVL = int.Parse(dtgNguyenVatLieu.CurrentRow.Cells[0].Value.ToString());
                nvl.SoLuong = int.Parse(txtSoLuong.Value.ToString());
                bool b = NguyenVatLieuBUS.Update_SoLuong(nvl);
                LoadNguyenVatLieu();
            }
            catch (Exception ex)
            {
                s = new SoundPlayer(Properties.Resources.Windows_Error);
                s.Play();
                MessageBox.Show(ex.Message);
            }
        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            LoadNguyenVatLieu();
            txtSoLuong.Value = 0;
        }
    }
}
